package com.snopy.concurrency.lock;

import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author snopy
 * @version 1.0.0
 * @classname LockInterrupt
 * @description
 * @email 77912204@qq.com
 * @date 2022/06/24 17:01
 */
@Slf4j
public class LockInterrupt {

    static final Lock lock = new ReentrantLock(true);
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
           log.info("t1 lock......");
           lock.lock();
           try {
               try {
                   TimeUnit.SECONDS.sleep(2);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               log.info("finish...");
           }finally {
               lock.unlock();
           }
        });
        Thread t2 = new Thread(()->{
            log.info(" t2lock......");
            synchronized (LockInterrupt.class){
                try {
                    TimeUnit.SECONDS.sleep(5);
                    log.info("t2 finish...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //t1.start();
        t2.start();
        TimeUnit.SECONDS.sleep(1);
        log.info("打断...");
        t2.interrupt();
        log.info("t2 state:{}",t1.getState());
        //t2.interrupt();

    }
}
